// 接口方法
import request from '@/utils/request'
// import { getStorage } from '@/utils/storage'
export * from './Article'
/**
 * 登陆接口
 * @param {*} paranm0 数据对象

 * @returns  promise对象
 */
export const loginAPI = ({ mobile, code }) => request({
  url: '/v1_0/authorizations',
  method: 'POST',
  data: {
    mobile: mobile,
    code: code
  }
})

/**
 * 获取已选频道
 *
 * @returns promise对象
 * 注意: 不轻质要求用户登录,匿名用户有磨人的已选频道
 */
export const getUserChannelListAPI = () => request({
  url: '/v1_0/user/channels'

})

/**
 * 获取所有频道列表
 * @returns promise对象
 *
 */
export const getChannelListAPI = () => {
  return request({
    url: '/v1_0/channels'
  })
}
// 请求频道列表高级写法
// export const getChannelListAPI = () => request({url: '/v1_0/channels'})

/**
 * 获取首页-文章列表
 * @param {*} channel_id 频道id
 * @returns promise对象
 */

export const getArticleListAPI = ({ channel_id, timestamp = new Date() }) => request({
  url: '/v1_0/articles',

  params: {
    channel_id,
    timestamp
  }
})
// 文章详情
export const getArticleDetailAPI = ({ artId }) => request({
  url: `/v1_0/articles/${artId}`

})
//  文章- 关注作者
export const followedAuthorAPI = (authorId) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: authorId
  }
})

//  文章- 取消关注作者
export const unFollowedAuthorAPI = (authorId) => request({
  url: `/v1_0/user/followings/${authorId}`,
  method: 'DELETE'

})

// 文章-点赞
export const likeArticleAPI = (artId) => request({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: artId
  }
})
// 文章-取消点赞
export const unLikeArticleAPI = (artId) => request({
  url: `/v1_0/article/likings/${artId}`,
  method: 'DELETE'
})

// 搜索--联想列表
export const suggestListAPI = (keyWord) => request({
  url: '/v1_0/suggestion',
  params: {
    q: keyWord
  }
})

// 搜索结果列表
export const searchResultAPI = ({ keyWord, page = 1, per_page = 10 }) => request({
  url: '/v1_0/search',
  params: {
    q: keyWord,
    page: page,
    pet_page: per_page
  }
})

// 重置频道,覆盖式保存
export const setUserChannelAPI = (channelArr) => request({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: {
    channels: channelArr
  }
})
// 删除频道
export const removeChannelAPI = (channelId) => request({
  url: `/v1_0/user/channels/${channelId}`,
  method: 'DELETE'
})

// 用户 - 基本资料
export const userInfoAPI = () => {
  return request({
    url: '/v1_0/user'
  })
}

// 用户-个人资料(获取生日)
export const userProfileAPI = () => {
  return request({
    url: '/v1_0/user/profile'
  })
}

// 用户- 更新头像
// 注意: formObj的值必须是一个表单对象
// '{"a": 10, "b": 20}' // 对象格式的JSON字符串
// new FormData() // 表单对象
export const updatePhotoAPI = (formObj) => {
  return request({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: formObj
    // 如果你的请求体内容是表单对象, 浏览器会自动携带请求头Content-Type为multipart/form-data
  })
}

// 用户 - 更新资料
export const updateProfileAPI = ({ birthday, userName }) => {
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: {
      birthday: birthday,
      name: userName
    }
  })
}

// 用户 - 更新token
export const refreshTokenAPI = () => request({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    Authorization: 'Bearer' + localStorage.getItem('refresh_token')
  }
})
